﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Maps.Controls;
using SkiTracker.ViewModel;

namespace SkiTracker.View
{
    public partial class ReadMessagePage : PhoneApplicationPage
    {
        public ReadMessagePage()
        {
            InitializeComponent();


        }

        private MapViewModel ViewModel { get { return DataContext as MapViewModel; } }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            
            string id;
            NavigationContext.QueryString.TryGetValue("id", out id);
            DataContext = id != null ? new ReadMessageViewModel(int.Parse(id)) : new ReadMessageViewModel();

            ViewModel.Messenger.Register<LocationRectangle>(this, SetView);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            ViewModel.Messenger.Unregister<LocationRectangle>(this);

            base.OnNavigatedFrom(e);
        }

        private void SetView(LocationRectangle locationRectangle)
        {
            Map.SetView(locationRectangle);
        }
    }
}